package com.trainoo.mina.demo;

import com.trainoo.mina.codec.MessageCodecFactory;
import com.trainoo.mina.codec.MessagePack;
import com.trainoo.mina.filter.MyFirstClientFilter;
import com.trainoo.mina.filter.MyLastClientFilter;
import com.trainoo.mina.handler.MyClientHandler;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.future.IoFuture;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

/**
 * @Auther: zt
 * @Date: 2020/4/14 15:19
 * @Description:
 */
public class MinaClientDemo {

    // 定义IP地址
    private static final String MINA_HOST = "127.0.0.1";
    // 定义端口
    private static final int MINA_PORT = 22222;

    public static void main(String[] args) {
        // 创建一个非阻塞的客户端
        NioSocketConnector ioConnector = new NioSocketConnector();

        // 测试使用默认编码解码器
        // testTextCodec(ioConnector);


        // 测试自定义编码节码器
        testMyCodec(ioConnector);
    }


    /**
     * 默认配置
     */
    private static void testTextCodec(IoConnector ioConnector){
        // 设置超时时间
        ioConnector.setConnectTimeoutMillis(30000);
        // 设置编码解码器
        ioConnector.getFilterChain().addLast("codec", new ProtocolCodecFilter(
                new TextLineCodecFactory(
                        Charset.forName("UTF-8"),
                        LineDelimiter.WINDOWS.getValue(),
                        LineDelimiter.WINDOWS.getValue()
                )
        ));
        // 绑定逻辑处理类
        ioConnector.setHandler(new MyClientHandler());
        // 创建连接
        ConnectFuture future = ioConnector.connect(new InetSocketAddress(MINA_HOST, MINA_PORT));
        // 等待连接创建完成
        future.awaitUninterruptibly();
        // 获取连接session
        IoSession ioSession = future.getSession();
        // 发送消息
        ioSession.write("Hello, mina");

        // 等待关闭连接
        ioSession.getCloseFuture().awaitUninterruptibly();
        ioConnector.dispose();
    }


    /**
     * 使用自定义编码解码器
     */
    private static void testMyCodec(IoConnector ioConnector){
        // 设置超时时间
        ioConnector.setConnectTimeoutMillis(30000);

        // 设置自定义编码解码器
        ioConnector.getFilterChain().addLast("codec", new ProtocolCodecFilter(
                new MessageCodecFactory(Charset.forName("UTF-8"))
        ));
        // 添加自定义过滤器
        ioConnector.getFilterChain().addFirst("firstFilter", new MyFirstClientFilter());
        ioConnector.getFilterChain().addLast("lastFilter", new MyLastClientFilter());

        // 绑定逻辑处理类
        ioConnector.setHandler(new MyClientHandler());
        // 创建连接
        ConnectFuture future = ioConnector.connect(new InetSocketAddress(MINA_HOST, MINA_PORT));

        // 这里采用监听方式获取session
        future.addListener(new IoFutureListener<IoFuture>() {
            @Override
            public void operationComplete(IoFuture future) {
                IoSession ioSession = future.getSession();
                // 发送10次
                for (int i = 0; i < 10; i++) {
                    String content = "测试数据：" + i;
                    MessagePack pack = new MessagePack(content);
                    ioSession.write(pack);
                }
            }
        });
    }
}
